//
//  GQHPreExpenseController.m
//  Xanthium
//
//  Created by GuanQinghao on 14/05/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHPreExpenseController.h"
#import "GQHPreExpenseView.h"
#import "GQHContractPaymentModel.h"
#import "GQHHeader.h"

@interface GQHPreExpenseController () <GQHPreExpenseViewDelegate> {
    
#pragma mark Data
    NSString *gasID;
    NSString *ETCID;
    NSString *cashID;
#pragma mark View
    /// 自定义根视图
    GQHPreExpenseView *rootView;
}

@end


@implementation GQHPreExpenseController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHPreExpenseView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    NSArray *paymentArray = _qh_orderModel.qh_contractPrepayment;
    
    for (GQHContractPaymentModel*paymentModel in paymentArray) {
        
        if ([paymentModel.qh_paymentType isEqualToString:@"01"]) {
            
            [rootView.qh_contentArray qh_safetySetObject:paymentModel.qh_paymentAmount atIndex:0];
            gasID = paymentModel.qh_paymentID;
        }
        if ([paymentModel.qh_paymentType isEqualToString:@"02"]) {
            
            [rootView.qh_contentArray qh_safetySetObject:paymentModel.qh_paymentAmount atIndex:1];
            ETCID = paymentModel.qh_paymentID;
        }
        if ([paymentModel.qh_paymentType isEqualToString:@"03"]) {
            
            [rootView.qh_contentArray qh_safetySetObject:paymentModel.qh_paymentAmount atIndex:2];
            cashID = paymentModel.qh_paymentID;
        }
        [rootView.qh_contentArray qh_safetySetObject:_qh_orderModel.qh_contractPrepaymentSum atIndex:3];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"预付款信息";
    [self.qh_rightButton setTitle:@"确认" forState:UIControlStateNormal];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

#pragma mark --Delegate
- (void)qh_selectedRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {
            
        case 0: {
            
            /// 油卡
            [self qh_alertWithTitle:@"请输入油卡金额" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"油卡金额";
                textField.keyboardType = UIKeyboardTypeDecimalPad;
                
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                /// 所列费用
                float preGas = [inputTextField.text floatValue];
                float preETC = [rootView.qh_contentArray[1] floatValue];
                float preCash = [rootView.qh_contentArray[2] floatValue];
                
                /// 预付款
                float preTotal = preGas + preETC + preCash;
                
                /// 预付款更新
                NSString *preTotalString = [NSString stringWithFormat:@"%.2f",preTotal];
                [rootView.qh_contentArray qh_safetySetObject:preTotalString atIndex:3];
                
                [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:0];
                [rootView.qh_tableView reloadData];
            } completion:nil];
        }
            break;
        case 1: {
            
            /// ETC
            [self qh_alertWithTitle:@"请输入ETC金额" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"ETC金额";
                textField.keyboardType = UIKeyboardTypeDecimalPad;
                
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                /// 所列费用
                float preGas = [rootView.qh_contentArray[0] floatValue];
                float preETC = [inputTextField.text floatValue];
                float preCash = [rootView.qh_contentArray[2] floatValue];
                
                /// 预付款
                float preTotal = preGas + preETC + preCash;
                
                /// 预付款更新
                NSString *preTotalString = [NSString stringWithFormat:@"%.2f",preTotal];
                [rootView.qh_contentArray qh_safetySetObject:preTotalString atIndex:3];
                
                [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:1];
                [rootView.qh_tableView reloadData];
            } completion:nil];
        }
            break;
        case 2: {
            
            /// 现金
            [self qh_alertWithTitle:@"请输入现金金额" message:nil inputHandler:^(UITextField * _Nullable textField) {
                
                textField.placeholder = @"现金金额";
                textField.keyboardType = UIKeyboardTypeDecimalPad;
                
            } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                
                UITextField *inputTextField = alertController.textFields.firstObject;
                /// 所列费用
                float preGas = [rootView.qh_contentArray[0] floatValue];
                float preETC = [rootView.qh_contentArray[1] floatValue];
                float preCash = [inputTextField.text floatValue];
                
                /// 预付款
                float preTotal = preGas + preETC + preCash;
                
                /// 预付款更新
                NSString *preTotalString = [NSString stringWithFormat:@"%.2f",preTotal];
                [rootView.qh_contentArray qh_safetySetObject:preTotalString atIndex:3];
                
                [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:2];
                [rootView.qh_tableView reloadData];
            } completion:nil];
        }
            break;
        default:
            break;
    }
}

#pragma mark --TargetMethod
- (void)qh_clickedRightButton:(UIButton *)sender {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:USER_GET_ID forKey:@"ownerId"];
    [parameters setObject:_qh_orderModel.qh_orderNumber forKey:@"waybillAwb"];
    
    NSMutableArray *prepaymentArray = [NSMutableArray array];
    NSMutableDictionary *preGasDictionary = [NSMutableDictionary dictionary];
    [preGasDictionary setObject:@"01" forKey:@"paymentType"];
    [preGasDictionary setObject:rootView.qh_contentArray[0] forKey:@"amount"];
    [preGasDictionary setObject:@"01" forKey:@"paymentTerms"];
    [preGasDictionary setObject:@"" forKey:@"reserved1"];
    [preGasDictionary setObject:gasID forKey:@"id"];
    [prepaymentArray addObject:preGasDictionary];
    
    NSMutableDictionary *preETCDictionary = [NSMutableDictionary dictionary];
    [preETCDictionary setObject:@"02" forKey:@"paymentType"];
    [preETCDictionary setObject:rootView.qh_contentArray[1] forKey:@"amount"];
    [preETCDictionary setObject:@"01" forKey:@"paymentTerms"];
    [preETCDictionary setObject:@"" forKey:@"reserved1"];
    [preETCDictionary setObject:ETCID forKey:@"id"];
    [prepaymentArray addObject:preETCDictionary];
    
    NSMutableDictionary *preCashDictionary = [NSMutableDictionary dictionary];
    [preCashDictionary setObject:@"03" forKey:@"paymentType"];
    [preCashDictionary setObject:rootView.qh_contentArray[2] forKey:@"amount"];
    [preCashDictionary setObject:@"01" forKey:@"paymentTerms"];
    [preCashDictionary setObject:@"" forKey:@"reserved1"];
    [preCashDictionary setObject:cashID forKey:@"id"];
    [prepaymentArray addObject:preCashDictionary];
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:prepaymentArray options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    [parameters setObject:jsonString forKey:@"jsonString"];
    
    NSString *APIURLString = [[NSString qh_APIURLString] stringByAppendingPathComponent:@"/buWaybill/1/surePretend"];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [GQHNetHelper qh_requestGET:APIURLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        DLog(@"%@---%@---%@",code,message,data);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (0 == [code integerValue]) {
            
            [self showToastWithText:message];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [self.navigationController popViewControllerAnimated:YES];
            });
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}

#pragma mark --PrivateMethod

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
